//
//  StatusCellTopView.swift
//  xinlangweibo
//
//  Created by yangshuo on 15/12/4.
//  Copyright © 2015年 yangshuo. All rights reserved.
//

import UIKit
//防止魔法梳子,在这里定义变量值
class StatusCellTopView: UIView {
    //定义一个视图模型来为cell设置数据
    var viewModel: StatusViewModel?{
        didSet{
            //姓名
          namelabel.text = viewModel?.status.user?.screen_name
            //placeholderImage如果用户头像未加载下来,显示占位头像
          iconView.sd_setImageWithURL(viewModel?.userProfile_image_url, placeholderImage: viewModel?.userDeafaultImage)
            //会员图标
            memberIconView.image = viewModel?.userMemberIconView
            //认证图标
            vipIconView.image = viewModel?.userVerified_type
            //TODO: 待修改
            //时间标签
            timeLabel.text = "刚刚"
            //来源标签
            sourceLabel.text = "黑马微博"
            
        }
    }
    
//指定构造函数
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //懒加载控件(顺序:从左到右,从上到下)
    //头像
    private lazy var iconView: UIImageView = UIImageView(imageName: "avatar_default_big")
    //姓名
    private lazy var namelabel: UILabel = UILabel(title: "加油", fontSize: 14)
    //会员图标
    private lazy var memberIconView: UIImageView = UIImageView(imageName: "common_icon_membership")
    //认证图标
    private lazy var vipIconView: UIImageView = UIImageView(imageName: "avatar_vip")
    //时间标签
    private lazy var timeLabel: UILabel = UILabel(title: "刚刚", color: UIColor.orangeColor(), fontSize: 11)
    //来源标签
    private lazy var sourceLabel: UILabel = UILabel(title: "黑马微博", fontSize: 11)
    
}
//MARK: - 设置界面
extension StatusCellTopView{
    private func setupUI(){
        //0.顶部视图分隔view
        let v = UIView()
        v.backgroundColor = UIColor.darkGrayColor()
        //添加控件分隔view
        addSubview(v)
        //分隔view的自动布局
        v.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top)
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
            // oh no 这个地方需要注意了属于重复约束,view中设置好了高
            //而且在iconview中top equalto v.bottom,所以不需要这个约束了!!!
          //  make.bottom.equalTo(iconView.snp_top)
            make.height.equalTo(statusMargin)
        }
        

        //1.添加控件
        addSubview(iconView)
        addSubview(namelabel)
        addSubview(memberIconView)
        addSubview(vipIconView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        //2.自动布局
        //头像
        iconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(v.snp_bottom).offset(statusMargin)
            make.left.equalTo(self.snp_left).offset(statusMargin)
            make.width.equalTo(statusWidth)
            make.height.equalTo(statusWidth)
        }
        //姓名
        namelabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(iconView.snp_top)
            make.left.equalTo(iconView.snp_right).offset(statusMargin)
        }
        //会员图标
        memberIconView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(namelabel.snp_top)
            make.left.equalTo(namelabel.snp_right).offset(statusMargin)
        }
        //认证图标
        vipIconView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView.snp_right)
            make.centerY.equalTo(iconView.snp_bottom)
        }
        //时间标签
        timeLabel.snp_makeConstraints { (make) -> Void in
            make.bottom.equalTo(iconView.snp_bottom)
            make.left.equalTo(iconView.snp_right).offset(statusMargin)
        }
        //来源标签
        sourceLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(timeLabel.snp_top)
            make.left.equalTo(timeLabel.snp_right).offset(statusMargin)
        }
    }
}